package org.strieber.fca.mid3;

import java.util.ArrayList;
import java.util.List;

public class GenericNode<T> {

	public T data;
	public List<Node<T>> children;
	
	// create an empty node
	public GenericNode() {
		
	}
	// create a node with data
	public GenericNode(T data) {
		this();
		setData(data);
	}
	// return this node's data
	public T getData() {
		return data;
	}
	// set this node's data
	public void setData(T data) {
		this.data = data;
	}
	// get the children of this node
	public List<Node<T>> getChildren() {
		if (children == null) {
			return new ArrayList<Node<T>>();
		}
		return children;
	}
	// set the children
	public void setChildren(List<Node<T>> children) {
		this.children = children;
	}
	// return the number of children
	public int getNumberOfChildren() {
		if (children == null) {
			return 0;
		}
		return children.size();
	}
	// add a child
	public void addChild(Node<T> child) {
		if (children == null) {
			children = new ArrayList<Node<T>>();
		}
		children.add(child);
	}	
	// insert a node at a specific position
	public void insertChildAt(int index, Node<T> child) {
		if (index == getNumberOfChildren()) {
			// really an append
			addChild(child);
			return;
		}
		else {
			// unchecked out of bounds exception
			children.add(index, child);
		}
	}
	// remove a certain child with a certain index
	public void removeChildAt(int index) {
		children.remove(index);
	}
	
	// toString method
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{").append(getData().toString()).append(", [");
		int i = 0;
		for (Node<T> e: getChildren()) {
			if (i > 0) {
				sb.append(",");
			}
			sb.append(e.getData().toString());
			i++;
		}
		sb.append("]").append("}");
		return sb.toString();
	}
	
}